//
//  UserProfileCard.swift
//  GithubUserSet
//
//  Created by Golden Jellyfish on 2025/9/2.
//

import SwiftUI

/*
struct UserProfileCard: View {
    
    @StateObject private var viewModel: UserProfileViewModel = UserProfileViewModel()
    
    var body: some View {
        
        switch viewModel.state {
        case .loading:
            ProgressView()
                .padding(40)
        case .success(let user):
            VStack(alignment: .leading, spacing: 16) {
                Image(systemName: "person.circle.fill")
                    .font(.system(size: 60))
                    .foregroundColor(.blue)
                
                // 从 ViewModel 中读取数据
                Text(user.name)
                    .font(.title)
                    .fontWeight(.bold)
                
                Text(user.bio ?? "No bio available")
                    .font(.body)
                    .foregroundColor(.gray)
                
                FollowButton(viewModel: viewModel)
            }
            .padding()
            .background(Color(UIColor.systemGray6))
            .cornerRadius(12)
            .shadow(radius: 5)
            .padding()
        case .failure(let error):
            VStack(spacing: 20) {
                Text("😔")
                    .font(.largeTitle)
                Text("Failed to load user.")
                Text(error.localizedDescription)
                    .font(.caption)
                    .foregroundColor(.gray)
                Button("Retry") {
                    // 点击重试按钮时，再次调用 fetch 方法
                    /*
                     Task {
                     await
                     viewModel.fetchUser(username: "apple")
                     }
                     */
                }
                .buttonStyle(.borderedProminent)
            }
            .padding()
            
        }
            .task {
                await viewModel.fetchUser(username: "apple")
            }
    }
    
}
*/

struct UserProfileCard: View {
    
    @StateObject private var viewModel = UserProfileViewModel()
    @State private var fetchTask: Task<Void, Never>?
    let username: String = "apple"
    var body: some View {
        // 根据 ViewModel 的 state 来决定显示什么
        Group {
            switch viewModel.state {
                
            case .loading:
                ProgressView() // SwiftUI 自带的加载动画
                    .padding(40)

            case .success(let user):
                // 这个就是我们之前写好的 UI
                VStack(alignment: .leading, spacing: 16) {
                    Image(systemName: "person.circle.fill")
                        .font(.system(size: 60))
                    Text(user.name)
                        .font(.title.bold())
                    Text(user.bio ?? "No bio available.") // 处理可选值
                        .foregroundColor(.gray)
                }
                .padding()

            case .failure(let error):
                VStack(spacing: 20) {
                    Text("😔")
                        .font(.largeTitle)
                    Text("Failed to load user.")
                    Text(error.localizedDescription)
                        .font(.caption)
                        .foregroundColor(.gray)
                    Button("Retry") {
                        // 点击重试按钮时，再次调用 fetch 方法
                        Task {
                            await viewModel.fetchUser(username: "apple")
                        }
                    }
                    //.buttonStyle(.borderedProminent)
                }
                .padding()
            }
        }
        .onAppear {
                    // 2. 在 .onAppear 中启动 Task 并赋值
                    fetchTask = Task {
                        await viewModel.fetchUser(username: self.username)
                    }
                }
                .onDisappear {
                    // 3. 在 .onDisappear 中取消 Task
                    fetchTask?.cancel()
                }
        // 当视图出现时，执行异步的 fetchUser 任务
        /*
        .task {
            await viewModel.fetchUser(username: "anomot")
        }
         */
        
    }
}

#Preview {
    UserProfileCard()
}
